package com.xuecheng.media.controller;

import com.xuecheng.api.media.model.MediaApi;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.convert.EntityMapper;
import com.xuecheng.media.entity.MediaDTO;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService  mediaService;

    /*
    保存媒资信息
     */
    @Override
    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO vo) {

        MediaDTO mediaDTO = EntityMapper.INSTANCE.mediaVoToDTO(vo);
        Long companyId = SecurityUtil.getCompanyId();
        mediaDTO.setCompanyId(companyId);

        return mediaService.createMedia(mediaDTO);
    }

    /**
     * 查询媒资信息
     * @param params
     * @param model
     * @return
     */
    @Override
    @PostMapping("media/list")
    public PageVO selectMedia(PageRequestParams params, @RequestBody QueryMediaModel model) {

        Long companyId = SecurityUtil.getCompanyId();

        return mediaService.selectMedia(params,model,companyId);
    }

    /**
     * 预览视频
     * @param mediaId 媒资id
     * @return
     */
    @Override
    @GetMapping(path = {"media/preview/{mediaId}","m/media/preview/{mediaId}"})
    public String selectMediaVideo(@PathVariable Long mediaId) {

        Long companyId = SecurityUtil.getCompanyId();

        return mediaService.selectMediaVideo(mediaId,companyId);
    }

    /**
     * 删除媒资信息
     * @param mediaId 媒资ID
     */
    @Override
    @DeleteMapping("media/{mediaId}")
    public void deleteMedia(@PathVariable Long mediaId) {

        Long companyId = SecurityUtil.getCompanyId();

        mediaService.deleteMedia(mediaId,companyId);
    }


    /**
     * 查询媒资信息--Feign
     * @param mediaId
     * @return
     */
    @Override
    @GetMapping("l/media/{mediaId}")
    public RestResponse<MediaDTO> selectMediaId4s(@PathVariable Long mediaId) {

        return mediaService.selectMediaId4s(mediaId);
    }


}
